package docview;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.StringTokenizer;
/**
 *
 * @author Admin
 */
public class PasteAction extends Action
{
    private int colS = -1;
    private int colE = -1;
    private int rowS = -1;
    private int rowE = -1;
    public void actionPerformed(ActionEvent e)
    {
        int[]rows = table.getSelectedRows();
        int[]cols = table.getSelectedColumns();
        if (rows.length == 0 && cols.length == 0) return;
        else if (rows.length > 0 && cols.length > 0 )
        {
            rowS = rows[0];
            colS = cols[0];
            rowE = rows[rows.length-1];
            colE = cols[cols.length-1];
        }
        Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
        try
        {
            String trstring = (String) system.getContents(this).getTransferData(DataFlavor.stringFlavor);
            StringTokenizer st1 = new StringTokenizer(trstring, "\n");
            StringTokenizer st2 ;
            int rowst = st1.countTokens();
            int ccounter = 0;
            int rcounter = 0;
            int maxcol = 0;
            StringTokenizer st3 = new StringTokenizer(trstring, "\n");
            while (st3.hasMoreTokens())
            {
                    String token = st3.nextToken();
                    st2 = new StringTokenizer(token, "\t");
                    if (st2.countTokens() > maxcol)
                    {
                        maxcol = st2.countTokens();
                    }
            }
            if (maxcol <= ((colE-colS)+1) && rowst <= ((rowE-rowS)+1))
            {
                st1 = new StringTokenizer(trstring, "\n");
                int rc = 0;
                int cc = 0;
                while (st1.hasMoreTokens())
                {
                    String token = st1.nextToken();
                    st2 = new StringTokenizer(token, "\t");
                    while (st2.hasMoreTokens())
                    {
                        String tkn = st2.nextToken();
                        table.setValueAt(tkn, rowS+rc, colS+cc);
                        String str = (String) table.getValueAt(rowS+rc, colS+cc);
                        if (str.equals("null"))
                        {
                            table.setValueAt("", rowS+rc, colS+cc);
                        }
                        cc++;
                    }
                    cc =0;
                    rc++;
                }
            }
            if (colS == colE && rowE == rowS && colE !=-1)
            {
                while (st1.hasMoreTokens())
                {
                    String token = st1.nextToken();
                    st2 = new StringTokenizer(token, "\t");
                    while (st2.hasMoreTokens())
                    {
                        String tkn = st2.nextToken();
                        if (table.isInTable( colS+ccounter, rowS+rcounter))
                        {
                            table.setValueAt(tkn, rowS+rcounter, colS+ccounter);
                            String str = (String) table.getValueAt(rowS+rcounter, colS+ccounter);
                            if (str.equals("null"))
                            {
                                table.setValueAt("", rowS+rcounter, colS+ccounter);
                            }
                            ccounter++;
                        }
                    }
                    ccounter = 0;
                    rcounter++;
                }
            }
        }
        catch (UnsupportedFlavorException ex)
        {
           return;
        } catch (IOException ex)
        {
           return;
        }
    }
}
